package com.falidos.controle.persistencia;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;

import com.falidos.modelo.artefatos.Artefato;
import com.falidos.utilidades.Utilidades;

public class Persistencia {
	
	public Persistencia(){}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean persistir(HashMap dados, String acao){			
		String nomeDao = "com.falidos.modelo.dao.";
		nomeDao += dados.get("botSubmit") + "DAO";
		Artefato artefato = new Utilidades().preparaArtefato(dados);
		try {
			Class daoClass = Class.forName(nomeDao);			
			Object dao = daoClass.newInstance();
			Method meth = daoClass.getMethod(acao, new Class[]{Artefato.class });
			boolean resp = (Boolean) meth.invoke(dao,artefato);
			return resp;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return false;		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public LinkedList pesquisas(HashMap dados, String[] campos, String[] operador, String[] chaves, String[] operLogico){	
		LinkedList<Artefato> lista = null;
		String nomeDao = "com.falidos.modelo.dao.";
		nomeDao += dados.get("botSubmit") + "DAO";		
		try {
			Class daoClass = Class.forName(nomeDao);
			Object dao = daoClass.newInstance();
			Method meth = daoClass.getMethod("pesquisar", new Class[]{String[].class, String[].class, String[].class, String[].class});//Procura um metod na classe
			lista = (LinkedList) meth.invoke(dao, campos, operador, chaves, operLogico);
			return lista;	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return lista;
	}

}
